// 金额格式化
Number.prototype.toMoney = function() {
  var arr = this.toFixed(2).toString().split('.')

  return arr[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + '.' + arr[1]
};

(function($, window, document, undefined) {

  /* 侧边栏按钮 */
  $(document).on('click', '.fixed-sidebar-item', function(e) {
    var $el = $(e.target)

    if (!$el.is('.fixed-sidebar-item')) {
      $el = $el.parents('.fixed-sidebar-item')
    }

    if ($el.hasClass('_top') || $el.find('.fixed-sidebar-bound').length) {
      e.preventDefault()
    }
  })
  /* /侧边栏按钮 */

  /* 返回顶部 */
  
  ;(function() {
    var $document  = $(document),
        $body      = $('html, body'),
        $fixed_top = $('#sidebar-top')
    
    // 显示返回顶部的距离
    var showTopLimit = 200

    // 节流
    $(window).on('scroll', _.debounce(checkTopDistance, 100))
    
    $(document).on('click', '#sidebar-top', function(e) {
      e.preventDefault()

      $body.animate({scrollTop: 0}, 150)
    })
    
    function checkTopDistance() {
      if ($document.scrollTop() >= showTopLimit) {
        $fixed_top.addClass('enabled')
      } else {
        $fixed_top.removeClass('enabled')
      }
    }

  })()
  
  /* /返回顶部 */
  
  /* 自定义下拉列表框 */
  ;(function() {

    // 获得焦点时显示
    $('.form-control-select .form-control').on('focus', function(e) {
      var $el = $(e.target),
          $p  = $el.parents('.form-control-select'),
          $d  = $p.find('ul')

      $p.addClass('active')
    })

    $(document).on('click', '.form-control-select li', function(e) {
      e.preventDefault()

      var $el  = $(e.target),
          $p   = $el.parents('.form-control-select'),
          $ipt = $p.find('.form-control'),
          $hid = $p.find('[type="hidden"]'),
          text = '',
          val  = ''

      if (!$el.is('li')) {
        $el = $el.parents('li')
      }

      text = $el.text()
      val  = $el.attr('value')

      $ipt.val(text)
      $hid.val(val)

      $p.removeClass('active')

    })

    // 点击空白隐藏
    $(document).on('click', function(e) {

      var $el = $(e.target)

      if ($el.closest('.form-control-select').length === 0) {
        $('.form-control-select').removeClass('active')
      }

    })
  })()
  /* 自定义下拉列表框 */

})(jQuery, window, document)